package com.creatorblue.modules.service.impl;

import com.creatorblue.common.constant.ScheduleConstants;
import com.creatorblue.modules.domain.SysJob;
import com.creatorblue.common.exception.TaskException;
import com.creatorblue.modules.persistence.SysJobMapper;
import com.creatorblue.modules.service.SysJobService;
import com.creatorblue.modules.utils.ScheduleUtils;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author huj
 * @create 2022-01-17 10:22
 * @email hujun@creatorblue.com
 */
@Service
public class SysJobServiceImpl implements SysJobService {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private SysJobMapper sysJobMapper;

    /**
     * 项目启动时，初始化定时器 主要是防止手动修改数据库导致未同步到定时任务处理（注：不能手动修改数据库ID和任务组名，否则会导致脏数据）
     */
    @PostConstruct
    public void init() throws SchedulerException, TaskException  {
        log.info("项目启动时，初始化定时器 主要是防止手动修改数据库导致未同步到定时任务处理");
        scheduler.clear();
        List<SysJob> jobList = sysJobMapper.selectJobAll();
        System.out.println(jobList);
    }

    /**
     * 根据条件 获取quartz调度器的计划任务
     *
     * @param job 调度信息
     * @return
     */
    @Override
    public List<SysJob> selectJobList(SysJob job) {
        return null;
    }

    /**
     * 通过调度任务ID查询调度信息
     *
     * @param jobId 调度任务ID
     * @return 调度任务集合
     */
    @Override
    public SysJob selectJobById(Long jobId) {
        return null;
    }

    /**
     * 暂停任务
     *
     * @param job
     * @return
     * @throws SchedulerException
     */
    @Override
    public int pauseJob(SysJob job) throws SchedulerException {
        Long jobId = job.getJobId();
        String jobGroup = job.getJobGroup();
        job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = sysJobMapper.updateJob(job);
        if (rows > 0) {
            scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
        }
        return rows;
    }

    /**
     * 恢复任务
     *
     * @param job
     * @return
     * @throws SchedulerException
     */
    @Override
    public int resumeJob(SysJob job) throws SchedulerException {
        return 0;
    }

    /**
     * 删除任务后，所对应的trigger也将被删除
     *
     * @param job
     * @return
     * @throws SchedulerException
     */
    @Override
    public int deleteJob(SysJob job) throws SchedulerException {
        return 0;
    }

    /**
     * 批量删除调度信息
     *
     * @param jobIds
     * @throws SchedulerException
     */
    @Override
    public void deleteJobByIds(Long[] jobIds) throws SchedulerException {

    }

    /**
     * 任务调度状态修改
     *
     * @param job
     * @return
     * @throws SchedulerException
     */
    @Override
    public int changeStatus(SysJob job) throws SchedulerException {
        return 0;
    }

    /**
     * 立即运行任务
     *
     * @param job
     * @throws SchedulerException
     */
    @Override
    public void run(SysJob job) throws SchedulerException {

    }

    /**
     * 新增任务
     *
     * @param job
     * @return
     * @throws SchedulerException
     * @throws TaskException
     */
    @Override
    public int insertJob(SysJob job) throws SchedulerException, TaskException {
        job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = sysJobMapper.insertJob(job);
        if (rows > 0) {
            ScheduleUtils.createScheduleJob(scheduler, job);
        }
        return rows;
    }

    /**
     * 更新任务
     *
     * @param job
     * @return
     * @throws SchedulerException
     * @throws TaskException
     */
    @Override
    public int updateJob(SysJob job) throws SchedulerException, TaskException {
        return 0;
    }

    /**
     * 校验cron表达式是否有效
     *
     * @param cronExpression
     * @return
     */
    @Override
    public boolean checkCronExpressionIsValid(String cronExpression) {
        return false;
    }
}
